package cn.schoolwow.quickdao.module.database.dml.json.service.save;

import cn.schoolwow.quickdao.module.database.dml.json.flow.common.check.CheckUniqueFieldNameFlow;
import cn.schoolwow.quickdao.module.database.dml.json.flow.insert.get.GetCountByUniqueFieldFlow;
import cn.schoolwow.quickdao.module.database.dml.json.service.insert.InsertJSONObjectCompositeBusiness;
import cn.schoolwow.quickdao.module.database.dml.json.service.update.UpdateJSONObjectCompositeBusiness;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SaveJSONObjectFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        flowContext.executeFlowList(
                new CheckUniqueFieldNameFlow(),
                new GetCountByUniqueFieldFlow()
        );

        Long count = (Long) flowContext.getData("count");
        if(null==count||count==0){
            flowContext.executeCompositeFlowList(new InsertJSONObjectCompositeBusiness());
        }else{
            flowContext.executeCompositeFlowList(new UpdateJSONObjectCompositeBusiness());
        }
    }

    @Override
    public String name() {
        return "保存JSONObject";
    }
}
